package org.jks.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jks.config.Config;
import org.jks.model.Group;
import org.jks.request.model.UIIds;
import org.jks.response.model.ActionResult;
import org.jks.service.impl.GroupServiceImpl;
import org.jks.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/group")
public class GroupController {
	@Resource(name = "GroupServiceImpl")
	private GroupServiceImpl groupImpl;
	
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request,
			HttpServletResponse response, HttpSession session,
			Map<String, Object> model) {
		List<Group> groups = groupImpl.all();
		model.put("groups", groups);
		Config.init(request, response, session, model, 0);
		return new ModelAndView("user/role", model);
	}
	
	@RequestMapping("/all")
	public void all(HttpServletRequest request,
			HttpServletResponse response, HttpSession session,
			Map<String, Object> model) {
		ActionResult ar = new ActionResult();
		try{
			List<Group> groups = groupImpl.all();
			ar.setData(groups);
			ar.setSuccess(true);
		}
		catch(Exception e){
			
		}
		finally{
			ResponseUtil.returnJson(response, ar);
		}
	}
	
	@RequestMapping("/add")
	public void add(Group group, HttpServletRequest request,  HttpSession session, HttpServletResponse response,
			Map<String, Object> model){
		ActionResult ar = new ActionResult();
		try{
			groupImpl.save(group);
			ar.setData(group);
			ar.setSuccess(true);
		}
		catch(Exception e){
		}
		finally{
			ResponseUtil.returnJson(response, ar);
		}
	}
	
	@RequestMapping("/update")
	public void update(Group group, HttpServletRequest request,  HttpSession session, HttpServletResponse response,
			Map<String, Object> model){
		ActionResult ar = new ActionResult();
		try{
			groupImpl.update(group);
			ar.setData(group);
			ar.setSuccess(true);
		}
		catch(Exception e){
		}
		finally{
			ResponseUtil.returnJson(response, ar);
		}
	}
	@RequestMapping("/delete")
	public void delete(UIIds ids, HttpServletRequest request,  HttpSession session, HttpServletResponse response,
			Map<String, Object> model){
		
	}
}
